
const { ccclass, property, menu } = cc._decorator;

@ccclass("LangPos")
export class LangPos {
    @property()
    lang: string = "";

    @property()
    pos: cc.Vec2 = new cc.Vec2(0, 0);
}

/** 不同语言节点位置调整 */
@ccclass
@menu('GlobalUI/LanguagePosFix')
export default class LanguagePosFix extends cc.Component {

    @property(cc.Node)
    private readonly labelNode: cc.Node = null;

    @property([LangPos])
    private readonly posList: LangPos[] = [];


    private posDef: cc.Vec2 = null;

    // LIFE-CYCLE CALLBACKS:

    protected onLoad(): void {
        if (this.mainNode)
            this.posDef = this.mainNode.getPosition();
        yy.event.on(this, yy.event.lang.LanuageChanged, this.updatePos);
        this.updatePos();
    }

    protected onEnable(): void {
        this.updatePos();
    }

    private get mainNode(): cc.Node {
        return this.labelNode || this.node;
    }

    private updatePos() {
        if (!this.mainNode || !this.posList.length) return;

        const curLang = yy.curLanguage;
        for (const info of this.posList) {
            if (info && info.lang == curLang) {
                this.mainNode.setPosition(info.pos);
                return;
            }
        }

        // default position
        this.mainNode.setPosition(this.posDef);
    }
}
